﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Configuration;
using Seemile.IDAL;
using Seemile.Common;
using Seemile.Model;
using System.Data.SqlClient;

namespace Seemile.DAL
{
    public class UserService : IUserService
    {
        #region 用户登录
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="_UsersInfo">当前登录的用户对象</param>
        /// <returns>成功则返回登录结果对象，否则返回null</returns>
        public UsersInfo UserLogin(UsersInfo _UsersInfo)
        {
            UsersInfo _UsersInfoResult = null;
            StringBuilder sql = new StringBuilder();
            sql.AppendLine(" SELECT ");
            sql.AppendLine("     [UserId], ");
            sql.AppendLine("     [Username], ");
            sql.AppendLine("     [Password], ");
            sql.AppendLine("     [Email], ");
            sql.AppendLine("     [Job], ");
            sql.AppendLine("     [Telephone] ");
            sql.AppendLine(" FROM [Users] ");
            sql.AppendLine(" WHERE ");
            sql.AppendLine("     [Email]=@Email ");
            sql.AppendLine("     AND [Password]=@Password ");

            SqlParameter[] sparas = new[] 
            {
                new SqlParameter("@Email",_UsersInfo.Email),
                new SqlParameter("@Password",_UsersInfo.Password)
            };

            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, System.Data.CommandType.Text, sql.ToString(), sparas))
            {
                if (reader.Read())
                {
                    int _UserId = (int)reader["UserId"];
                    string _Username = reader["Username"] as string;
                    string _Password = reader["Password"] as string;
                    string _Email = reader["Email"] as string;
                    string _Job = reader["Job"] as string;
                    string _Telephone = reader["Telephone"] as string;

                    _UsersInfoResult = new UsersInfo(_UserId, _Username, _Password, _Email, _Job, _Telephone);
                }
            }

            return _UsersInfoResult;
        }
        #endregion
    }
}
